package com.yuhanxun.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.dfish.downloadtoolNew.R;



import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.util.Log;

public class AppGlobalVariable extends Application {
	private static String SDCARD = "";
	private static String PACKAGE = "";
	private static String PACKAGE_DIR = "";

	private static String DOWNLOAD = "/dfish_download/";
	private static String DOWNLOAD_TEMP = "/dfish_download_temp/";

	private static String DIR_DOWNLOAD = "";
	private static String DIR_DOWNLOAD_TEMP = "";
	private static String SDcard_DOWNLOAD = "";
	
	public static final String ADDRESS_LOCAL_FILE_NAME = "address.txt";

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		setSDCARD(getSDPath());

		 PACKAGE = this.getApplicationInfo().packageName;
		 PACKAGE_DIR = "/data/data/"+PACKAGE+"/";
//		DIR_DOWNLOAD = SDCARD + DOWNLOAD;
//		DIR_DOWNLOAD_TEMP = SDCARD + DOWNLOAD_TEMP;
		DIR_DOWNLOAD = PACKAGE_DIR + DOWNLOAD;
		DIR_DOWNLOAD_TEMP = PACKAGE_DIR + DOWNLOAD_TEMP;
		

		DealFile.makeDir(new File(DIR_DOWNLOAD));
		DealFile.makeDir(new File(DIR_DOWNLOAD_TEMP));
		
		
		
		if(!SDCARD.equals("")) {
			SDcard_DOWNLOAD = SDCARD + DOWNLOAD;
			DealFile.makeDir(new File(SDcard_DOWNLOAD));
			DealFile.copyFileFromRaw(this, SDcard_DOWNLOAD + ADDRESS_LOCAL_FILE_NAME, R.raw.address);
		}
		DealFile.delAllFile(DIR_DOWNLOAD);
		String[] args1 = { "chmod", "777", DIR_DOWNLOAD};  
		exec(args1); 

	}

	public static String getDIR_DOWNLOAD_TEMP() {
		return DIR_DOWNLOAD_TEMP;
	}
	
	public static String getSDcard_DOWNLOAD(){
		return SDcard_DOWNLOAD;
	}

	public static String getSDCARD() {
		return SDCARD;
	}

	public static void setSDCARD(String sDCARD) {
		SDCARD = sDCARD;
	}

	public static String getDIR_DOWNLOAD() {
		return DIR_DOWNLOAD;
	}

	public static String getAppVersionName(Context context) {
		String versionName = "";
		try {
			// ---get the package info---
			PackageManager pm = context.getPackageManager();
			PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
			versionName = pi.versionName;
			if (versionName == null || versionName.length() <= 0) {
				return "";
			}
		} catch (Exception e) {
			Log.e("VersionInfo", "Exception", e);
		}
		return versionName;
	}

	public String getSDPath() {
		File sdDir = null;
		boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
		if (sdCardExist) {
			sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
		} else
			return "";
		return sdDir.toString();
	}

	public static boolean hasThatApk(Context context, String packageName) {
		final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
		List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
		List<String> pName = new ArrayList<String>();// 用于存储所有已安装程序的包名
		// 从pinfo中将包名字逐一取出，压入pName list中
		if (pinfo != null) {
			for (int i = 0; i < pinfo.size(); i++) {
				String pn = pinfo.get(i).packageName;
				pName.add(pn);
			}
		}
		return pName.contains(packageName);// 判断pName中是否有目标程序的包名，有TRUE，没有FALSE
	}

	public static boolean hasSDCard() {
		return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
	}


	
	/**
	 * @param con
	 * @return 检查3G网络
	 */
	public static boolean checkNetWork(Context con) {
		ConnectivityManager connMgr = null;
		if (null == connMgr) {
			connMgr = (ConnectivityManager) con.getSystemService(Context.CONNECTIVITY_SERVICE);
		}
		NetworkInfo mobileInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		if (mobileInfo.isAvailable()) {
			return true;
		} else {
			return false;
		}
	}
//	public static void setConfig(String path, String key, int value) {
//		DealCfg dc = new DealCfg(path);
//		dc.setValue(key, String.valueOf(value));
//		dc.saveFile();
//	}
	
    /** 执行Linux命令，并返回执行结果。 */
    public static String exec(String[] args) {
        String result = "";
        ProcessBuilder processBuilder = new ProcessBuilder(args);
        Process process = null;
        InputStream errIs = null;
        InputStream inIs = null;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int read = -1;
            process = processBuilder.start();
            errIs = process.getErrorStream();
            while ((read = errIs.read()) != -1) {
                baos.write(read);
            }
            baos.write('\n');
            inIs = process.getInputStream();
            while ((read = inIs.read()) != -1) {
                baos.write(read);
            }
            byte[] data = baos.toByteArray();
            result = new String(data);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (errIs != null) {
                    errIs.close();
                }
                if (inIs != null) {
                    inIs.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (process != null) {
                process.destroy();
            }
        }
        return result;
    }

}